<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Base 
 * +------------------------------------------------------------+
 * 框架内置基类/
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-16 15:04:46
 *
 */
abstract class Base {
	///当前正在操作的模块名称
	protected $_moduleName;
	
	///当前正在执行的动作名称
	protected $_actionName;
	
	///存储当前HTTP请求提交的id值
	protected $_id;
	
	//存储$_COOKIE
	public $cookie;
	
	//存储$_ENV
	public $env;
	
	//存储$_GET
	public $get;
	
	//存储$GLOBALS
	public $global;
	
	//存储$_POST
	public $post;
	
	//存储$_POST或$_GET，优先获取$_POST的值
	public $params;
	
	//存储$_REGQUEST
	public $request;
	
	//存储$_SERVER
	public $server;
	
	//存储$_SESSION
	public $session;
	
	//表单上提交上来的数据，包括url上的参数
	public $formdata;
	
	//构造函数
	public function __construct(){
		$this->_moduleName = $GLOBALS['MODULE_NAME'];
		$this->_actionName = $GLOBALS['ACTION_NAME'];
		
		$this->cookie	=	new _ArrayObj('cookie');
		$this->env		=	new _ArrayObj('env');
		$this->get		=	new _ArrayObj('get');
		$this->global	=	new _ArrayObj('global');
		$this->params	=	new _ArrayObj('params');
		$this->post		=	new _ArrayObj('post');
		$this->server	=	new _ArrayObj('server');
		$this->session	=	new _ArrayObj('session');
		$this->request	=   new _ArrayObj('request');
		
		$this->_id		=	(int) $this->id;
		$this->formdata =	extend($_GET, $_POST);
	}
	
	///判断是否为AjAX请求
    protected function isAjax() {
    	$varAjax = C('VAR_AJAX','ajax');
    	
    	$res = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
    		&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') 
    		|| (isset($_GET[$varAjax]) || isset($_POST[$varAjax]));
    	
    	return  $res;
    }

	///判断是否为get请求
	protected function isGet(){
		return 'get' == strtolower($_SERVER['REQUEST_METHOD']);
	}
	
	///判断是否为post请求
	protected function isPost(){
		return 'post' == strtolower($_SERVER['REQUEST_METHOD']);
	}
	
	/**
	 * 判定是否为微信浏览器
	 */
	protected function isMicroBrowser(){
		return stripos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
	}
	
	/**
	 * 是否是在IOS上设备上
	 */
	protected function isIos(){
		$USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
		
		return strstr($USER_AGENT,'iPhone') || strstr($USER_AGENT,'iPad');
	}
	
	//获取HTTP请求变量，获取顺序POST->GET
	public function getRequest($name){
		return isset($_POST[$name]) ? $this->post->$name : $this->get->$name;
	}
	
	public function __get($name){
		if (@property_exists($this, $this->$name)){
			return $this->$name;
		}
		
		if (isset($_POST[$name])){
			return $_POST[$name];
		}
		
		if (isset($_GET[$name])){
			return $_GET[$name];
		}
		
		if (isset($_REQUEST[$name])){
			return $_REQUEST[$name];
		}
		
		return false;
	}
	
	//析构函数
	public function __destruct(){
		
	}
}

//将数组转换为一个对象
class _ArrayObj{
	private $scope;
	
    public function __construct($scope)
    {
        $this->scope = $scope;
    }

    public function set($key, $value)
    {
    	switch ($this->scope){
    		case 'post':
    			$_POST[$key] = $value;
    			break;
    		case 'get':
    			$_GET[$key] = $value;
    			break;
    		case 'server':
    			$_SERVER[$key] = $value;
    			break;
    		case 'cookie':
    			$_COOKIE[$key] = $value;
    			break;
    		case 'params':
    			$_GET[$key] = $_POST[$key] = $value;
    			break;
    		case 'request':
    			$_REQUEST[$key] = $value;
    			break;
    		case 'session':
    			$_SESSION[$key] = $value;
    			break;
    		case 'env':
    			$_ENV[$key] = $value;
    			break;
    		case 'global':
    			$GLOBAL[$key] = $value;
    			break;
    	}
    }
    
	public function __set($key, $value){
		$this->set($key, $value);
	}
	
    public function __get($key)
    {
    	$value = false;
    	switch ($this->scope){
    		case 'post':
    			$value = isset($_POST[$key]) ? $_POST[$key] : null;
    			break;
    		case 'get':
    			$value = isset($_GET[$key]) ? $_GET[$key] : null;
    			break;
    		case 'server':
    			$value = isset($_SERVER[$key]) ? $_SERVER[$key] : (isset($_SERVER[strtoupper($key)]) ? isset($_SERVER[strtoupper($key)]) : null);
    			break;
    		case 'cookie':
    			$value = isset($_COOKIE[$key]) ? $_COOKIE[$key] : null;
    			break;
    		case 'params': 
    			$value = isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : null);
    			break;
    		case 'request': 
    			$value = isset($_REQUEST[$key]) ? $_REQUEST[$key] : null;
    			break;
    		case 'session':
    			$value = isset($_SESSION[$key]) ? $_SESSION[$key] : null;
    			break;
    		case 'env':
    			$value = isset($_ENV[$key]) ? $_ENV[$key] : null;
    			break;
    		case 'global':
    			$value = isset($GLOBALS[$key]) ? $GLOBALS[$key] : null;
    			break;
    	}
    	
        return $value;
    }
    
    public function __unset($key){
    	$var = '_' . strtoupper($this->scope);
    	global $$var;
    	unset($GLOBALS[$var][$key]);
    }
}
?>